package com.aptech.fpt.c0810g.newspj.beans;

import com.aptech.fpt.c0810g.newspj.dataaccess.NewsAction;
import com.aptech.fpt.c0810g.newspj.entity.News;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author anhgt_b00501
 */
@ManagedBean
@RequestScoped
public class KillIt {

    private FacesContext contect = FacesContext.getCurrentInstance();
    private News newsForDel;
    private String message;
    private boolean isDelete;
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isIsDelete() {
        return isDelete;
    }

    public void setIsDelete(boolean isDelete) {
        this.isDelete = isDelete;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public News getNewsForDel() {
        return newsForDel;
    }

    public void setNewsForDel(News newsForDel) {
        this.newsForDel = newsForDel;
    }

    /** Creates a new instance of KillIt */
    public KillIt() {
        getNewsForDelete();
    }

    public void getNewsForDelete() {
        ShareBean bean = (ShareBean) contect.getApplication().evaluateExpressionGet(contect, "#{shareBean}", ShareBean.class);
        NewsAction newsAction = new NewsAction();
        try {
            int newsID = Integer.valueOf(contect.getExternalContext().getRequestParameterMap().get("id"));
            bean.setId(newsID);
        } catch (Exception e) {
        }
        this.id = bean.getId();
        this.newsForDel = newsAction.getNewsListByID(this.id).get(0);
    }

    public void doDeleteNews() {
        try {
            NewsAction newsAction = new NewsAction();
            boolean deleteNews = newsAction.deleteNews(this.id);
            if (deleteNews) {
                this.message = "Đã xóa thành công !";
                contect.addMessage("message", new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", message));
            } else {
                this.message = "Không thể xóa, bạn hãy thử lại sau nhé!";
                contect.addMessage("message", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", message));
            }
        } catch (SQLException ex) {
         
        }
    }
}
